package com.blb.servlet;

import com.blb.pojo.User;
import com.blb.service.IUserService;
import com.blb.service.impl.UserServiceImpl;
import com.blb.utils.EncryptionUtils;
import com.blb.utils.SpringUtils;
import com.blb.utils.UUIDUtils;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet{
    private IUserService userService = SpringUtils.getBean(IUserService.class);

    /**
     * 注册
     * @param req
     * @param resp
     */
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String passwordCheck = req.getParameter("passwordCheck");
        String image = req.getParameter("image");

        try {
            User userByUsername = userService.getByUsername(username);
            if(userByUsername!=null){
                resp.getWriter().write("用户名已存在");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().write("用户名查询失败:" + e.getMessage());
        }

        if(!password.equals(passwordCheck)){
            resp.getWriter().write("两次密码不相等");
            return;
        }

        if(StringUtils.isNullOrEmpty(image)){
            image = "default";
        }

        try {
            userService.add(new User(UUIDUtils.getId(), username, EncryptionUtils.encryptMD5(password), image));
            resp.getWriter().write("ok");
        } catch (SQLException e) {
            e.printStackTrace();
            resp.getWriter().write("注册失败:" + e.getMessage());
        }
    }

    /**
     *检查用户名是否存在
     * @param req
     * @param resp
     */
    public void usernameCheck(HttpServletRequest req, HttpServletResponse resp){
        String username = req.getParameter("username");
        try {
            User userByUsername = userService.getByUsername(username);
            if(userByUsername != null){
                resp.getWriter().write("该用户名已存在");
                return;
            }
            resp.getWriter().write("ok");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 用户登录
     * @param req
     * @param resp
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        try {
            user = userService.getByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
            req.setAttribute("msg", "查询用户名错误");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            return;
        }

        if(user==null){
            req.setAttribute("msg", "用户名不存在");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            return;
        }

        if(!EncryptionUtils.encryptMD5(password).equals(user.getPassword())){
            req.setAttribute("msg", "密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req, resp);
            return;
        }

        req.getSession().setAttribute("user", user);
        resp.sendRedirect("/moments/topic/list");

    }

}
